const {nanoid}=require("nanoid")
const {sqlConnect}=require("../model/index")
let express = require("express");
infoRouter=express.Router()
infoRouter.post('/admin/airline-info',async function(req,res){
    console.log("info POST请求到了服务器,向数据库插入一条新数据")
    console.log(req.body)
    var {data}=req.body
    const id=nanoid(10)
    const new_set={id,...data}
    console.log(new_set)
    var sql=`insert into airline_info set ?`
    sqlConnect.query(sql,new_set,function (err) {
        if(err){
          console.log('[INSERT ERROR] - ',err.message);
          return;
        }
       res.json({
        success: true
      })
  })
})
infoRouter.put('/admin/airline-info/:id',async function(req,res){
    console.log(req.params)
    console.log("info PUT请求到了服务器,向数据库更改一条数据")
    const {id}=req.params
    const new_set={id,...req.body}
    console.log(new_set)
    var sql=`update airline_info SET ? where id='${id}'`
    sqlConnect.query(sql,new_set,function(err,result){
        if(err){
            console.log('[UPDATE ERROR] - ',err.message);
            return;
        }
        res.json({
            success:true
        })
    })
})
infoRouter.get('/admin/airline-info',function(req,res){
    var sql = `select 
    airline_info.id,
    DATE_FORMAT(s_time,'%Y-%m-%d') as s_date,
    DATE_FORMAT(s_time,'%H:%m') as s_time,
    DATE_FORMAT(e_time,'%Y-%m-%d') as e_date,
    DATE_FORMAT(e_time,'%H:%m') as e_time, 
    flight_id,
    a.location_name as s_name,
    b.location_name as e_name,
    capacity,
    price
    from location as a,location as b,airline_info
    where a.id=airline_info.s_loc_id and b.id=airline_info.e_loc_id
    `;
    sqlConnect.query(sql,function (err, result) {
        if(err){
          console.log('[SELECT ERROR] - ',err.message);
          return;
        }
       console.log('--------------------------SELECT----------------------------');
       console.log(result);
       console.log('------------------------------------------------------------\n\n');  
       res.json({
        data:result,
        success: true,
      });
    })
    console.log("info GET请求到了服务器")
})
infoRouter.get('/admin/airline-info/search',async function(req,res){
    console.log('info GET-search请求到了服务器')
    console.log(req.query)
    var sql=`select 
    airline_info.id,
    DATE_FORMAT(s_time,'%Y-%m-%d') as s_date,
    DATE_FORMAT(s_time,'%H:%m') as s_time,
    DATE_FORMAT(e_time,'%Y-%m-%d') as e_date,
    DATE_FORMAT(e_time,'%H:%m') as e_time, 
    flight_id,
    a.location_name as s_name,
    b.location_name as e_name,
    capacity,
    price
    from location as a,location as b,airline_info
    where a.id=airline_info.s_loc_id and b.id=airline_info.e_loc_id
    `
    const {flight_id,s_loc_id,s_time}=req.query
    console.log(flight_id,s_loc_id,s_time)
    sql+=flight_id?`and flight_id="${flight_id}"`:''
    sql+=s_loc_id?`and s_loc_id="${s_loc_id}"`:''
    sql+=s_time?`and DATE_FORMAT(s_time,'%Y-%m-%d')="${s_time}"`:''
    console.log(sql)
    await sqlConnect.query(sql,function (err, result) {
        if(err){
          console.log('[SELECT ERROR] - ',err.message);
          return;
        }
       console.log('--------------------------SELECT----------------------------');
       console.log(result);
       console.log('------------------------------------------------------------\n\n');  
       res.json({
        data:result,
        success: true,
      })
    })
})
// infoRouter.post(`/admin/airline-info/${id}`,function(req,res){
//     console.log(req.body)
// })
module.exports = {
   infoRouter
  };
  